# -*- coding: utf-8 -*-
# @Time : 2022-01-06 10:03
# @Author : Z
# @Function :

import random
import re

# 导入浏览器驱动，控制浏览器
from selenium import webdriver
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from Common.Logger import path

# 实例化浏览器对象driver，打开浏览器，
driver = webdriver.Chrome()
# driver.maximize_window()
# driver.get('http://www.baidu.com')
# driver.refresh()
# # driver.find_element_by_xpath('//a[text()="新闻"]').click()
# driver.get('https://www.hao123.com/')
# driver.set_window_size(540, 960)
# driver.maximize_window()
# driver.back()
# time.sleep(1)
# driver.forward()
# time.sleep(1)
# driver.get_screenshot_as_file('../lib/b1.png')

# xpath定位，id,name,text,模糊匹配
# driver.find_element_by_xpath('//input[@name="wd"]').send_keys('123')
# driver.find_element_by_xpath('//input[@name="wd"]').send_keys(Keys.CONTROL, 'a')
# time.sleep(1)
# driver.find_element_by_xpath('//input[@name="wd"]').send_keys(Keys.CONTROL, 'x')
# driver.find_element_by_xpath('//input[@name="wd"]').send_keys(Keys.CONTROL, 'c')
# driver.find_element_by_xpath('//input[@name="wd"]').clear()
# time.sleep(1)
# driver.find_element_by_xpath('//input[@name="wd"]').send_keys(Keys.CONTROL, 'v')
# time.sleep(1)
# driver.find_element_by_xpath('//input[@name="wd"]').clear()
# time.sleep(1)
# driver.find_element_by_xpath('//input[@name="wd"]').send_keys('123')
# driver.find_element_by_xpath('//input[@id="su"]').submit()    # 模拟回车键
# driver.find_element_by_xpath('//input[@id="su"]').click()
# ele01 = driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')
# action=ActionChains(driver)
# action.move_to_element(ele01).perform()
# ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')).perform()
# driver.find_element_by_xpath('//a[contains(text(),"搜索设置")]').click()
# driver.find_element_by_xpath('//input[@id="nr_2"]').click()

# 鼠标右击
# ele02=driver.find_element_by_xpath('//*[@id="s-top-loginbtn"]')
# ActionChains(driver).context_click(ele02).perform()

# h = driver.current_window_handle
# time.sleep(1)
# driver.find_element_by_xpath('//a[@class="mnav c-font-normal c-color-t"]/../a[2]').click()
# time.sleep(1)
# all_h = driver.window_handles
# driver.switch_to.window(all_h[1])
# print(all_h[0])
# print(all_h[1])
# driver.close()
# time.sleep(1)
# driver.switch_to.window(all_h[0])
# print(driver.title)

# time.sleep(1)
# driver.implicitly_wait(10)
# driver.find_element_by_id("kw").send_keys(u"测试部落")
# driver.find_element_by_id("kw").submit()
# # s = driver.find_elements_by_css_selector("h3.t>a")
# s = driver.find_elements_by_xpath('//*[@id="content_left"]/div/h3/a')
# # for i in s:
# #     print(i.get_attribute('href'))
# # # 设置随机值
# t = random.randint(0, 9)
# # 随机取一个结果点击鼠标
# s[t].click()
# time.sleep(1)

# iframe的切换，动态的id，需提取id,切换iframe后，需要退出iframe
# driver.get("http://mail.163.com/")
# driver.maximize_window()
# driver.implicitly_wait(10)
# a = driver.find_element_by_xpath('//*[@id="loginDiv"]/iframe').get_attribute('id')
# print(a)
# driver.switch_to.frame(a)
# driver.find_element_by_xpath('//input[@name="email"]').send_keys('15678300490')
# driver.find_element_by_xpath('//input[@name="password"]').send_keys('7812977zwx')
# driver.find_element_by_xpath('//*[@id="dologin"]').click()
# time.sleep(5)
# driver.switch_to.default_content()
# driver.find_element_by_xpath('//*[@id="_mail_tree_1_156"]').click()

# 切换到弹窗
# url = "http://localhost:63342/test009/lib/file/test.html?_ijt=skf8c1tkqfodhh05nmdsiju7ll"
# print(url)
# driver.get(url)
# alert
# driver.find_element_by_xpath('//*[@id="alert"]').click()
# alert = driver.switch_to.alert
# print(alert.text)
# time.sleep(1)
# # alert.accept()  # 相当于确认按钮
# alert.dismiss()  # 取消alert,相当于点击x按钮

# confirm
# driver.find_element_by_xpath('//*[@id="confirm"]').click()
# time.sleep(1)
# t = driver.switch_to.alert
# print(t.text)
# # t.accept()
# t.dismiss()

# prompt
# driver.find_element_by_xpath('//*[@id="prompt"]').click()
# t1 = driver.switch_to.alert
# print(t1.text)
# time.sleep(1)
# t1.send_keys('hello world')
# t1.accept()

# 单选框
# driver.get('file:///C:/Users/15678/Desktop/test02.html')
# s = driver.find_element_by_xpath('//*[@id="boy"]').is_selected()  # 判断是否已选择上
# print(s)
# driver.find_element_by_xpath('//*[@id="boy"]').click()
# s1 = driver.find_element_by_xpath('//*[@id="boy"]').is_selected()
# print(s1)
# time.sleep(2)
# driver.find_element_by_xpath('//*[@id="girl"]').click()
# # 多选框
# f = driver.find_elements_by_xpath('//*[@type="checkbox"]')
# for i in f:
#     print(i.get_attribute('id'))
#     i.click()

# table表格定位
# driver.get('file:///C:/Users/15678/Desktop/test02%20(2).html')
# s = driver.find_element_by_xpath('//*[@id="myTable"]/tbody/tr[2]/td[1]')
# print(s.text)


# # 登陆163邮箱
# driver.get("http://mail.163.com/")
# driver.maximize_window()
# driver.implicitly_wait(10)
# a = driver.find_element_by_xpath('//*[@id="loginDiv"]/iframe').get_attribute('id')
# print(a)
# driver.switch_to.frame(a)
# driver.find_element_by_xpath('//input[@name="email"]').send_keys('15678300490')
# driver.find_element_by_xpath('//input[@name="password"]').send_keys('7812977zwx')
# driver.find_element_by_xpath('//*[@id="dologin"]').click()
# time.sleep(5)
# driver.switch_to.default_content()
#
# driver.find_element_by_xpath('(//*[text()="首页"])[1]/../../li[6]/b').click()
# time.sleep(1)
# driver.find_element_by_xpath('(//*[@class="nui-menu-item-link"])[6]').click()
#
# # 进入写信
# driver.find_element_by_xpath('//*[@id="dvNavTitle"]/../div[1]/ul/li[2]').click()
# driver.find_element_by_xpath('(//*[text()="收件人"]/../..//input)[2]').send_keys('15678300490@163.com')
# driver.find_element_by_xpath('//*[text()="主　题"]/../..//input').send_keys('15678300490@163.com')
# time.sleep(1)
# driver.find_element_by_xpath('//*[text()="添加附件"]/../..//input').send_keys(path+r'\lib\imgs\logo.png')
# print(path+'/lib/imgs/logo.png')
# ll = driver.find_element_by_xpath('//div[@class="APP-editor-edtr"]/iframe')
# print(ll.get_attribute('class'))
# driver.switch_to.frame(ll)
# driver.find_element_by_xpath('//*[@name="referrer"]/../../body').send_keys('111123456')
# driver.switch_to.default_content()
# driver.find_element_by_xpath('(//*[text()="发送"])[2]').click()
# time.sleep(2)

# 获取元素属性
# driver.get('http://www.baidu.com')
# driver.maximize_window()
# print(driver.title)
# print(driver.name)
# a = driver.find_element_by_xpath('//*[text()="新闻"]').text
# print(a)
# b = driver.find_element_by_xpath('//*[@id="kw"]').tag_name
# print(b)
# c = driver.find_element_by_xpath('//*[@id="kw"]').get_attribute('id')
# print(c)

# 爬页面源码
# driver.get('http://www.baidu.com')
# driver.maximize_window()
# page = driver.page_source
# print(page)
# "非贪婪匹配,re.S('.'匹配字符,包括换行符)"
# url_list = re.findall('href=\"(.*?)\"', page, re.S)
# url_all = []
# for url in url_list:
#     if "http" in url:
#         print(url)
#         url_all.append(url)
# print(url_all)

# cookies
# a1 = driver.get_cookies()
# print(a1)
# # 登陆163邮箱
# driver.get("http://mail.163.com/")
# b = driver.get_cookies()
# print(b)
# driver.maximize_window()
# driver.implicitly_wait(10)
# a = driver.find_element_by_xpath('//*[@id="loginDiv"]/iframe').get_attribute('id')
# print(a)
# driver.switch_to.frame(a)
# driver.find_element_by_xpath('//input[@name="email"]').send_keys('15678300490')
# driver.find_element_by_xpath('//input[@name="password"]').send_keys('7812977zwx')
# driver.find_element_by_xpath('//*[@id="dologin"]').click()
# time.sleep(5)
# driver.switch_to.default_content()
# c = driver.get_cookies()
# print(c)


# js 操作滚动条
# driver.get('http://www.baidu.com')
# driver.maximize_window()
# driver.find_element('id','kw').send_keys('测试')
# driver.find_element('id','su').click()
# time.sleep(1)
# # # 滑到底部
# driver.execute_script("document.documentElement.scrollTop=10000")
# time.sleep(1)
# # 滚动到顶部
# time.sleep(1)
# driver.execute_script("document.documentElement.scrollTop=0")
# # 移动到指定位置
# time.sleep(1)
# ll = "window.scrollTo(100,400);"
# driver.execute_script(ll)
# time.sleep(1)
# # 定位元素滚动至可见区域
# al = driver.find_element_by_xpath('//*[text()="下一页 >"]')
# driver.execute_script("arguments[0].scrollIntoView()", al)

# # js处理日历控件（修改readonly属性），输入值，去掉属性值
# driver.get("https://kyfw.12306.cn/otn/index/init")
# # 去掉元素的readonly属性
# js = 'document.getElementById("train_date").removeAttribute("readonly");'
# driver.execute_script(js)
# # 用js方法输入日期
# js_value = 'document.getElementById("train_date").value="2021-11-15"'
# driver.execute_script(js_value)

# ll = driver.find_element_by_id("train_date")
# driver.execute_script('arguments[0].removeAttribute("readonly")', ll)
# driver.execute_script("arguments[0].value='2021-11-15'", ll)

# 输入会重复的，清不了输入的内容
# driver.find_element_by_id("train_date").clear()
# driver.find_element_by_id("train_date").send_keys("2021-11-15")


# 获取关联词
# driver.get('http://www.baidu.com')
# driver.find_element_by_xpath('//*[@id="kw"]').send_keys('测试')
# # bd = driver.find_elements_by_xpath('//*[@class="bdsug-overflow"]')
# time.sleep(1)
# bd = driver.find_elements_by_class_name("bdsug-overflow")

# for i in bd:
#     print(i.get_attribute('data-key'))
# if len(bd) > 1:
#     bd[1].click()
#     print(driver.current_url)
# else:
#     print("未匹配到关联词")


# driver.get("http://sh.xsjedu.org/")
# time.sleep(1)
# # js = 'document.getElementById("doyoo_monitor").style.display="none";'
# # driver.execute_script(js)   //*[@id="Orders_Tel2"]  //*[@id="btnsubmit2"]
# driver.find_element_by_xpath('//*[@id="Orders_Name2"]').send_keys('1234')
# driver.find_element_by_xpath('//*[@id="Orders_Tel2"]').send_keys('1234')
# driver.find_element_by_xpath('//*[@id="btnsubmit2"]').click()


# js点击，输入值，去掉属性值
# driver.get("http://cnblogs.com/yoyoketang")
# ll = driver.find_element_by_xpath('//*[@id="blog_nav_contact"]')
# driver.execute_script('arguments[0].click();', ll)
# # 输入账号  //*[@id="mat-input-0"]  //*[@id="mat-input-1"]
# l2 = driver.find_element_by_xpath('//*[@id="mat-input-0"]')
# driver.execute_script('arguments[0].value="123"', l2)
# l3 = driver.find_element_by_xpath('//*[@id="mat-input-1"]').send_keys('1234')
# driver.find_element_by_xpath('//*[text()=" 登录 "]').click()

# 鼠标事件悬停
# driver.get('http://www.baidu.com')
# shezhi = driver.find_element(By.XPATH,'//span[text()="设置" and @id]')
# time.sleep(1)
# action = ActionChains(driver)
# action.move_to_element(shezhi).perform()
# gg = driver.find_element(By.XPATH, '//span[text()="搜索设置"]')
# driver.execute_script('arguments[0].click()',gg)


# # 键盘输入
# driver.get('http://www.baidu.com')
# driver.find_element(By.XPATH,'//*[@name="wd"]').send_keys('1234')
# # ctrl+a
# driver.find_element(By.XPATH,'//*[@name="wd"]').send_keys(Keys.CONTROL,'a')
# time.sleep(1)
# # ctrl+x
# driver.find_element(By.XPATH,'//*[@name="wd"]').send_keys(Keys.CONTROL,'x')
# time.sleep(1)
# # ctrl+v
# driver.find_element(By.XPATH,'//*[@name="wd"]').send_keys(Keys.CONTROL,'v')

# css 定位
# driver.get('http://www.baidu.com')
# # 使用 # 代表id的属性, id=kw 用 #kw
# driver.find_element(By.CSS_SELECTOR, '#kw').send_keys('1234')
# # 使用 . 代表 class
# time.sleep(1)
# driver.find_element(By.CSS_SELECTOR, '.s_ipt').clear()
# # 其他属性  name  type  等等
# driver.find_element(By.CSS_SELECTOR, '[name="wd"]').send_keys('测试')

# 标签与属性组合使用
# driver.find_element(By.CSS_SELECTOR, 'input#kw').send_keys('1234')
# time.sleep(1)
# driver.find_element(By.CSS_SELECTOR, 'input.s_ipt').clear()
# time.sleep(1)
# driver.find_element(By.CSS_SELECTOR, 'input[name="wd"]').send_keys("测试")
# time.sleep(1)

driver.switch_to.frame()
driver.switch_to.default_content()
driver.window_handles()
driver.switch_to.window()

# driver.quit()

# 打开safari浏览器
# from selenium import webdriver
# driver = webdriver.Safari()
# driver.get('http://www.baidu.com')
# time.sleep(1)
# driver.maximize_window()
# driver.find_element(By.XPATH,'//*[text()="新闻"]').click()
# driver.maximize_window()
# handles=driver.window_handles
# print(handles)
# time.sleep(1)
# driver.switch_to.window(handles[1])
# time.sleep(1)
# driver.close()
# driver.switch_to.window(handles[0])
# driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('test')
# driver.find_element(By.XPATH,'//input[@id="su"]').click()
# time.sleep(2)
# driver.quit()


# driver = webdriver.Chrome()
# driver.maximize_window()
#
# driver.get('http://testingedu.com.cn:8000/index.php/Home/user/login.html')
# ele = driver.find_element(By.XPATH,'//*[@id="verify_code_img"]')
# time.sleep(1)
# print(ele.get_attribute('src'))
# print(path)
# ele.screenshot(path+r'\lib\verify\verify.png')




